package com.snap.vseries.receiver;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.orhanobut.logger.Logger;
import com.snap.vseries.ui.SplashActivity;


public class UpdateApkReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("Rachel_test", "UpdateApkReceiver监听到广播:");

        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;

        Logger.e("--------------------------------UpdateApkReceiver");
        Log.d("Rachel_test", "--------------------------------UpdateApkReceiver" + intent.getAction());
        Log.d("Rachel_test", "--------------------------------UpdateApkReceiver" + context.getPackageName());
        if ((intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED) || intent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED)) && !context.getPackageName().equals(cn.getPackageName())) {
            Intent intent2 = new Intent(context, SplashActivity.class);
            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent2);
        }
    }
}
